/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generatorGraphics.graphics;

import grammaticalClasses.nwConsole.ShowStatCur;
import grammaticalClasses.nwConsole.ShowStatDay;
import grammaticalClasses.nwConsole.ShowStatNet;
import grammaticalClasses.nwConsole.ShowStatShort;
import grammaticalClasses.nwConsole.ShowStatus;
import java.awt.Color;
import java.awt.Dimension;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.sql.rowset.serial.SerialException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;

/**
 *
 * @author Heitor
 */
public class ReliabilityXStability extends Graph{
    
    public ReliabilityXStability(){
        name = "ReliabilityXStability";
        help = "Help of ReliabilityXStability";
        
        configUnitOptions();
        commands.add("ShowStatNet");
        commands.add("ShowStatDay");
        commands.add("ShowStatCur");
        commands.add("ShowStatShort");
    }
    
    private void configUnitOptions(){
    
        unitList.add("Time per Day");
        unitList.add("Time 15 to 15");
        
    }
    
    @Override
    protected void createGraph(String unit){
        
        int indice;
        
//        if(search == null){
//            search = new SearchCommands(commands);
//            answer = (ArrayList<Object>) search.getCommandsAnswer();
//        }
        
        switch (unit){
            case "Time per Day":
                indice = search.getAnswer(commands, "ShowStatDay");

                chartPanel = new ChartPanel(buildGraphTDay((ArrayList<ShowStatDay>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                break;
            case "Time 15 to 15":
                indice = search.getAnswer(commands, "ShowStatCur");
                int indice2 = search.getAnswer(commands, "ShowStatShort");
                
                chartPanel = new ChartPanel(buildGraphT15to15((ArrayList<ShowStatCur>) answer.get(indice),
                        (ArrayList<ShowStatShort>) answer.get(indice2)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                break;
            default:
                chartPanel = new ChartPanel(null);
                break;
        }
    }
    
    
    private JFreeChart buildGraphTDay(ArrayList<ShowStatDay> auxStatDay){
            
        //XYSeries xyseries1 = new XYSeries("Reliability");
        //XYSeries xyseries2 = new XYSeries("Stability");
        TimeSeries timeseries1 = new TimeSeries("Reliability");
        TimeSeries timeseries2 = new TimeSeries("Stability");

        for(int i=0; i<auxStatDay.size(); i++){
            if(auxStatDay.get(i).getNetStats().size()>0){
                if((auxStatDay.get(i).getNetStats().get(0).getReliab() != null) ||
                    (auxStatDay.get(i).getNetStats().get(0).getStab() != null)){
//                    xyseries1.add(Integer.parseInt(auxStatDay.get(i).getidDay())
//                            ,auxStatDay.get(i).getNetStats().get(0).getReliab());
//                    xyseries2.add(Integer.parseInt(auxStatDay.get(i).getidDay())
//                            ,auxStatDay.get(i).getNetStats().get(0).getStab());
                    String[] aux = auxStatDay.get(i).getDataFinal().split("/");
                    if(aux.length == 3){
                        timeseries1.add(new Day(Integer.parseInt(aux[1]), Integer.parseInt(aux[0]),
                                Integer.parseInt(aux[2])+2000), auxStatDay.get(i).getNetStats().get(0).getReliab());
                        timeseries2.add(new Day(Integer.parseInt(aux[1]), Integer.parseInt(aux[0]),
                                Integer.parseInt(aux[2])+2000), auxStatDay.get(i).getNetStats().get(0).getStab());
                    }
                }
            }
        }
        
//        XYSeriesCollection xyseriescollection = new XYSeriesCollection();
//        xyseriescollection.addSeries(xyseries1);
//        XYDataset xydataset = xyseriescollection;
        
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(timeseries1);
        TimeSeriesCollection dataset2 = new TimeSeriesCollection();
        dataset2.addSeries(timeseries2);
        XYDataset timeXydataset = dataset;
        XYDataset timeXydataset2 = dataset2;
        
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Reliability versus Stability", "Days", "Reliability", timeXydataset, true, true, true);
        //JFreeChart jfreechart2 = ChartFactory.createXYLineChart("Qualidade da rede****", "Days", "Reliability", xydataset, PlotOrientation.VERTICAL, false, true, false);
       
        XYPlot xyplot2 = (XYPlot)jfreechart.getPlot();
        xyplot2.setDomainPannable(true);
        xyplot2.setRangePannable(true);
            NumberAxis numberaxis2 = new NumberAxis("Stability");
            numberaxis2.setAutoRangeIncludesZero(false);
        xyplot2.setRangeAxis(1, numberaxis2);
            
        //XYSeriesCollection xyseriescollection2 = new XYSeriesCollection();
        //xyseriescollection2.addSeries(xyseries2);
        //XYDataset xydataset2 = xyseriescollection2;
        
        xyplot2.setDataset(1, timeXydataset2);
        xyplot2.mapDatasetToRangeAxis(1, 1);
        
        XYItemRenderer xyitemrenderer = xyplot2.getRenderer();
        xyitemrenderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        if (xyitemrenderer instanceof XYLineAndShapeRenderer)
        {
            XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
            xylineandshaperenderer.setBaseShapesVisible(true);
            xylineandshaperenderer.setBaseShapesFilled(true);
        }
        XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer();
        xylineandshaperenderer1.setSeriesPaint(0, Color.black);
        xylineandshaperenderer1.setBaseShapesVisible(true);
        xylineandshaperenderer1.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        
        xyplot2.setRenderer(1, xylineandshaperenderer1);
        
        DateAxis axis = (DateAxis) xyplot2.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd/MMM/yyyy"));
        
        LegendTitle legendtitle = new LegendTitle(xyitemrenderer);
        LegendTitle legendtitle1 = new LegendTitle(xylineandshaperenderer1);
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        blockcontainer.add(legendtitle, RectangleEdge.LEFT);
        blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
        blockcontainer.add(new EmptyBlock(2000D, 0.0D));
        CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
        compositetitle.setPosition(RectangleEdge.BOTTOM);
        jfreechart.addSubtitle(compositetitle);
        ChartUtilities.applyCurrentTheme(jfreechart);
        
        jfreechart.setBackgroundPaint(new Color(238, 238, 238));
        return jfreechart;
    }
    
    private JFreeChart buildGraphT15to15(ArrayList<ShowStatCur> auxStatCur,
                        ArrayList<ShowStatShort> auxStatShort){

        TimeSeries timeseries1 = new TimeSeries("Reliability");
        TimeSeries timeseries2 = new TimeSeries("Stability");
        
        for(int i=0; i<auxStatShort.size(); i++){
            if(auxStatShort.get(i).getNetStats().size()>0){
                if((auxStatShort.get(i).getNetStats().get(0).getReliab() != null)||
                    (auxStatShort.get(i).getNetStats().get(0).getStab() != null)){
                    String[] aux = auxStatShort.get(i).getDataFinal().split("/");
                    String[] aux1 = auxStatShort.get(i).getHourFinal().split(":");
                    if((aux.length == 3) && (aux1.length == 3)){
                        try{
                        timeseries1.add(new Minute(Integer.parseInt(aux1[1]), Integer.parseInt(aux1[0]),
                            Integer.parseInt(aux[1]), Integer.parseInt(aux[0]),
                            Integer.parseInt(aux[2])+2000), auxStatShort.get(i).getNetStats().get(0).getReliab());
                        timeseries2.add(new Minute(Integer.parseInt(aux1[1]), Integer.parseInt(aux1[0]),
                            Integer.parseInt(aux[1]), Integer.parseInt(aux[0]),
                            Integer.parseInt(aux[2])+2000), auxStatShort.get(i).getNetStats().get(0).getStab());
                        } catch (SeriesException e){}
                    }
                }
            }
        }
        
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(timeseries1);
        TimeSeriesCollection dataset2 = new TimeSeriesCollection();
        dataset2.addSeries(timeseries2);
        XYDataset timeXydataset = dataset;
        XYDataset timeXydataset2 = dataset2;
        
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Reliability versus Stability",
            "15 to 15 min", "Reliability", timeXydataset, false, true, false);
        
        XYPlot xyplot2 = (XYPlot)jfreechart.getPlot();
        xyplot2.setDomainPannable(true);
        xyplot2.setRangePannable(true);
            NumberAxis numberaxis2 = new NumberAxis("Stability");
            numberaxis2.setAutoRangeIncludesZero(false);
        xyplot2.setRangeAxis(1, numberaxis2);
        
        xyplot2.setDataset(1, timeXydataset2);
        xyplot2.mapDatasetToRangeAxis(1, 1);
        
        XYItemRenderer xyitemrenderer = xyplot2.getRenderer();
        xyitemrenderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        if (xyitemrenderer instanceof XYLineAndShapeRenderer)
        {
            XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
            xylineandshaperenderer.setBaseShapesVisible(true);
            xylineandshaperenderer.setBaseShapesFilled(true);
        }
        XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer();
        xylineandshaperenderer1.setSeriesPaint(0, Color.black);
        xylineandshaperenderer1.setBaseShapesVisible(true);
        xylineandshaperenderer1.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        
        xyplot2.setRenderer(1, xylineandshaperenderer1);
        
        DateAxis axis = (DateAxis) xyplot2.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("hh:mm dd/MM/yy"));
        axis.setVerticalTickLabels(true);
        
        LegendTitle legendtitle = new LegendTitle(xyitemrenderer);
        LegendTitle legendtitle1 = new LegendTitle(xylineandshaperenderer1);
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        blockcontainer.add(legendtitle, RectangleEdge.LEFT);
        blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
        blockcontainer.add(new EmptyBlock(2000D, 0.0D));
        CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
        compositetitle.setPosition(RectangleEdge.BOTTOM);
        jfreechart.addSubtitle(compositetitle);
        ChartUtilities.applyCurrentTheme(jfreechart);
        
        jfreechart.setBackgroundPaint(new Color(238, 238, 238));
        return jfreechart;
    }
    
    @Override
    public ChartPanel getChartPanel() {
        
        if(search != null){
            createGraph("");
            return chartPanel;
        } else {
            return new ChartPanel(null);
        }
    }
    
}
